package com.aiwhose.aiwhosesystem.controller;

import cn.hutool.core.util.StrUtil;
import com.aiwhose.aiwhosecore.bean.PageBean;
import com.aiwhose.aiwhosecore.bean.Result;
import com.aiwhose.aiwhosesystem.utils.RedisUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.aiwhose.aiwhosecore.bean.system.Banner;
import com.aiwhose.aiwhosesystem.service.BannerService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 轮播图 控制层。
 *
 * @author 田国刚
 * @since 1.0.1
 */
@RestController
@Tag(name = "轮播图接口")
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 添加轮播图。
     *
     * @param banner 轮播图
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Operation(description = "保存轮播图")
    public boolean save(@RequestBody @Parameter(description = "轮播图") Banner banner) {

        boolean save = bannerService.save(banner);
        redisUtil.del("system:sysBanner:" + banner.getType());
        return save;
    }

    /**
     * 根据主键删除轮播图。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Operation(description = "根据主键轮播图")
    public boolean remove(@PathVariable @Parameter(description = "轮播图主键") Serializable id) {
        Banner banner = bannerService.getById(id);
        boolean remove = bannerService.removeById(id);
        redisUtil.del("system:sysBanner:" + banner.getType());
        return remove;
    }

    /**
     * 根据主键更新轮播图。
     *
     * @param banner 轮播图
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Operation(description = "根据主键更新轮播图")
    public boolean update(@RequestBody @Parameter(description = "轮播图主键") Banner banner) {
        boolean update = bannerService.updateById(banner);
        redisUtil.del("system:sysBanner:" + banner.getType());
        return update;
    }

    /**
     * 查询所有轮播图。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(description = "查询所有轮播图")
    public List<Banner> list() {
        return bannerService.list();
    }

    /**
     * 根据轮播图主键获取详细信息。
     *
     * @param id 轮播图主键
     * @return 轮播图详情
     */
    @GetMapping("getInfo/{id}")
    @Operation(description = "根据主键获取轮播图")
    public Banner getInfo(@PathVariable Serializable id) {
        return bannerService.getById(id);
    }

    /**
     * 分页查询轮播图。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(description = "分页查询轮播图")
    public Result page(@Parameter(description = "分页信息") Banner banner, PageBean page) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(Banner::getType, banner.getType(), StrUtil.isNotEmpty(banner.getType()));
        return Result.ok(bannerService.getMapper().paginate(page.getCurrent(), page.getPageSize(), queryWrapper));
    }
}
